package com.lbjhan.rpc.serializer;

import com.alibaba.fastjson.JSON;
import com.lbjhan.rpc.pojo.User;


public class FastjsonSerializer<T> implements ISerializer {
    @Override
    public byte[] serialize(Object obj){
        byte[] bytes = JSON.toJSONString(obj).getBytes();
        return bytes;
    }

    @Override
    public T deserialize(byte[] bytes, Class clazz) {
        T t = (T)JSON.parseObject(new String(bytes), clazz);
        return t;
    }

    public static void main(String[] args) {
        FastjsonSerializer<User> userFastjsonSerializer = new FastjsonSerializer<>();
        byte[] bytes = userFastjsonSerializer.serialize(new User(1, "tom"));
        System.out.println(bytes.length);
        User user = userFastjsonSerializer.deserialize(bytes, User.class);
        System.out.println(user);
        /**
         * 21
         * User(id=1, name=tom)
         */
    }
}
